package com.hainatong.shiphelper.module.warehouse;


import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.ActivityPortListBinding;
import com.hainatong.shiphelper.module.warehouse.adapter.SupplierAdapter;
import com.hainatong.shiphelper.module.warehouse.entity.SupplierBean;
import com.hainatong.shiphelper.module.warehouse.viewmodel.SupplierViewModel;
import com.hainatong.shiphelper.utils.RecyclerViewHelper;
import com.hainatong.shiphelper.widget.recycleviewdriver.ListItemDecoration;
import com.hainatong.mvvmbase.base.BaseMvvmActivity;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;

/**
 * 选择供应商 页面
 */
public class SupplierActivity extends BaseMvvmActivity<ActivityPortListBinding, SupplierViewModel>
{

    public static void start(Context context)
    {
        context.startActivity(new Intent(context, SupplierActivity.class));
        ((Activity) context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    public static String SUPPLIER_RESULT = "SupplierResult";

    private SupplierAdapter adapter;

    @Override
    protected int getLayoutId()
    {
        return R.layout.activity_port_list;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected void initView()
    {

        setToolbarTitle(viewDataBinding.includeTitle.toolbar,getString( R.string.select_port )  );
        setToolBar( viewDataBinding.includeTitle.toolbar );

        adapter = new SupplierAdapter();
        viewDataBinding.refreshlayout.setRefreshHeader(new ClassicsHeader(this));
        viewDataBinding.refreshlayout.setRefreshFooter(new ClassicsFooter(this));
        RecyclerViewHelper.addLinerLayoutMgr(viewDataBinding.recyclerview,new ListItemDecoration(SizeUtils.dp2px(10)) );
        viewDataBinding.recyclerview.setAdapter( adapter );

        adapter.setOnItemClickListener((adapter1, view1, position) -> {
            SupplierBean supBean = adapter.getItem( position );
            // 发送 选中的 港口
            LiveEventBus.get(SupplierActivity.SUPPLIER_RESULT , SupplierBean.class)
                        .post( supBean );

            finish();
        });
        viewDataBinding.refreshlayout.setOnRefreshListener( refreshLayout -> {
            viewModel.tryRefresh();
        } );
        viewDataBinding.refreshlayout.setOnLoadMoreListener( refreshLayout -> {
            viewModel.loadMore();
        } );

        setLoadSir( viewDataBinding.refreshlayout.getParent() );

        //搜索 按钮
        viewDataBinding.btnSearch.setOnClickListener( v -> {
            // 搜索
            String keyWord = viewDataBinding.etSearch.getText().toString().trim();
            if(ObjectUtils.isNotEmpty( keyWord ))
            {
                startSearchSupplierBykeyName( keyWord );
            }
        } );
        // 软键盘 上的 搜索 按钮
        viewDataBinding.etSearch.setOnKeyListener( (v, keyCode, event) -> {
            if(keyCode == KeyEvent.KEYCODE_ENTER)
            {
                String keyWordStr = viewDataBinding.etSearch.getText().toString().trim();
                if(!TextUtils.isEmpty( keyWordStr ))
                {
                    KeyboardUtils.hideSoftInput( SupplierActivity.this );
                    startSearchSupplierBykeyName( keyWordStr );
                }
                return  true;
            }
            return false;
        } );


    }

    @Override
    protected void initData()
    {
        showLoading();
        // 查询 供应商
        viewModel.querySupplier("");

        // list 结果
        viewModel.getSupplierResult().observe(this,result -> {
            if( result.isSuccess() )
            {
                if( result.isRefresh() )
                {
                    showContent();
                    viewDataBinding.refreshlayout.finishRefresh(true);
                    if( result.getData() != null )
                    {
                        adapter.setList(result.getData());
                    }
                }
                else
                {
                    viewDataBinding.refreshlayout.finishLoadMore(true);
                    if( result.getData() != null )
                    {
                        adapter.addData(result.getData());
                    }
                }
            }
            else
            {
                if( result.isRefresh() )
                {
                    showFailure(result.getMsg());
                    //刷新时 加载失败
                    //ToastUtils.showShort( result.getMsg() );
                }
                else
                {
                    if( result.isLoadMoreEmpty() )
                    {
                        viewDataBinding.refreshlayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });


    }

    @Override
    protected void onRetryBtnClick()
    {
        viewModel.querySupplier("");
    }


    private void startSearchSupplierBykeyName(String keyName)
    {
        viewModel.querySupplier(keyName);
    }


    private void setToolbarTitle(Toolbar toolbar, String title)
    {
        // toolbar.setBackgroundColor(ContextCompat.getColor(this,R.color.red));

        ((TextView)toolbar.findViewById(R.id.tv_title)).setText(title);
        ((ImageButton)toolbar.findViewById(R.id.btn_back)).setImageResource(R.drawable.svg_back);
        toolbar.findViewById(R.id.btn_back).setOnClickListener(v -> {
            supportFinishAfterTransition();
        });
    }

}
